package com.hhhn.wk.main.tab4.pwd;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.utils.AllPublic;
import com.hhhn.wk.utils.Constants;
import com.hhhn.wk.utils.MD5Util;
import com.hhhn.wk.utils.mi.AES;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
/**
 * * * * * * * * * * * *
 * @author：文珂    ｡◕‿◕｡ *
 * Data：2018/1/2 0002    ｡◕‿◕｡ *
 * Function：      ｡◕‿◕｡ *
 * 修改密码
 * * * * * * * * * * * */

public class UpPwdActivity extends BaseActivity {
    private LinearLayout ll_dq;
    private TextView tv_title;
    private TextView tv_userPhone;
    private TextView tv_getCode;//获取验证码
    private TextView et_code;//验证码
    private EditText et_pwd;//密码
    private EditText et_phone;//账号
    private Button btn_ok;//完成
    private Context context;
    private BaseActivity activity;
    private Intent intent=null;
    private int type=0;
    @Override
    protected void setOnCreateContentView() {
        setContentView(R.layout.activity_up_pwd0);
        context=UpPwdActivity.this;
        activity=this;
        intent=getIntent();
        type = intent.getIntExtra("type", 1);
    }
    @Override
    protected void initView() {
        et_phone=findViewById(R.id.et_phone);
        et_code=findViewById(R.id.et_code);
        btn_ok=findViewById(R.id.btn_ok);
        et_pwd=findViewById(R.id.et_pwd);
        tv_getCode=findViewById(R.id.tv_getCode);
        tv_title=findViewById(R.id.tv_title);
        tv_userPhone=findViewById(R.id.tv_userPhone);
        ll_dq=findViewById(R.id.ll_dq);
    }
    @Override
    protected void initData() {
        tv_title.setText("设置登录密码");
//        Log.d("修改密码", "initData: "+getUser().toString());
        if (type==0){
            //未登录
            et_phone.setVisibility(View.VISIBLE);
            ll_dq.setVisibility(View.GONE);
        }else {
            et_phone.setVisibility(View.GONE);
            ll_dq.setVisibility(View.VISIBLE);
            //已登录
            tv_userPhone.setText(getUser().getUserPhone());
        }

    }
    @Override
    protected void initClick() {
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type==0){
                    //未登录
                    if (et_phone.getText().toString().trim()==null||"".equals(et_phone.getText().toString().trim())){
                        toastWk("手机号不能为空");
                        return;
                    }
                }
                if (et_pwd.getText().toString().trim()==null||"".equals(et_pwd.getText().toString().trim())){
                    toastWk("密码不能为空");
                    return;
                }
                if (et_code.getText().toString().trim()==null||"".equals(et_code.getText().toString().trim())){
                    toastWk("验证码不能为空");
                    return;
                }
                getChangepsd();
            }
        });
        tv_getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getCode();
            }
        });
    }
    //获取验证码
    public void getCode() {
        showLoad();
        RequestParams params = AllPublic.getParamsValid(activity, "/getValidate", false);
        if (type==0){
            try {
                JSONObject person=new JSONObject();
                person.put("doType",2);/*验证码*/
                person.put("phone",et_phone.getText().toString().trim());/*手机号*/
                AES aes = AllPublic.jsonDesPackaging(person);
                params.addParameter("data",aes.getData());
                params.addParameter("key",aes.getKey());
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }else {
            try {
                JSONObject person=new JSONObject();
                person.put("doType",2);/*验证码*/
                person.put("phone",getUser().getUserPhone());/*手机号*/
                AES aes = AllPublic.jsonDesPackaging(person);
                params.addParameter("data",aes.getData());
                params.addParameter("key",aes.getKey());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


        //请求
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override  //成功
            public void onSuccess(String decode) {
                String result = AllPublic.jsonDesDecode(decode);
                try {
                    JSONObject jsonObject= new JSONObject(result);
                    String reason = jsonObject.getString("code");
                    if (reason.equals("0")) {
                        Toast.makeText(context,"获取成功", Toast.LENGTH_SHORT).show();
                        final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000, 1000);
                        myCountDownTimer.start();
                    }else {
                        toastWk(jsonObject.getString(getResources().getString(R.string.errMsg)));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dismissLoad();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("获取验证码-第二个", ex + "");
                ex.printStackTrace();
                dismissLoad();
            }
            @Override
            public void onCancelled(CancelledException cex) {
                Log.d("获取验证码-第三个", cex + "");
                dismissLoad();
            }
            @Override
            public void onFinished() {
                Log.d("获取验证码-第四个", "onFinished");
                dismissLoad();
            }
        });
    }
    //复写倒计时
    private class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            tv_getCode.setClickable(false);
            tv_getCode.setText(l/1000+"s");
        }
        //计时完毕的方法
        @Override
        public void onFinish() {
            //重新给Button设置文字
            tv_getCode.setText("重新获取");
            //设置可点击
            tv_getCode.setClickable(true);
        }
    }
    //修改密码
    public void getChangepsd() {
        showLoad();
        RequestParams params = AllPublic.getParamsAccount(activity, "/changepsd", false);
        if (type==0){
            try {
                JSONObject person=new JSONObject();
                person.put("doType",2);/*验证码*/
                person.put("phone",et_phone.getText().toString().trim());/*手机号*/
                person.put("code",et_code.getText().toString().trim());/*验证码*/
                person.put("password", MD5Util.md5(et_pwd.getText().toString().trim()));/*密码*/
                AES aes = AllPublic.jsonDesPackaging(person);
                params.addParameter("data",aes.getData());
                params.addParameter("key",aes.getKey());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else {
            //已登录
            try {
                JSONObject person=new JSONObject();
                person.put("doType",2);/*验证码*/
                person.put("phone",getUser().getUserPhone());/*手机号*/
                person.put("code",et_code.getText().toString().trim());/*验证码*/
                person.put("password", MD5Util.md5(et_pwd.getText().toString().trim()));/*密码*/
                AES aes = AllPublic.jsonDesPackaging(person);
                params.addParameter("data",aes.getData());
                params.addParameter("key",aes.getKey());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


//
//        try {
//            JSONObject person=new JSONObject();
//            person.put("doType",2);/*验证码*/
//            person.put("phone",et_phone.getText().toString().trim());/*手机号*/
//            person.put("phone",getUser().getUserPhone());/*手机号*/
//            person.put("code",et_code.getText().toString().trim());/*验证码*/
//            person.put("password", MD5Util.md5(et_pwd.getText().toString().trim()));/*密码*/
//            params.addParameter("body",AllPublic.jsonDesPackaging(person));
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        //请求
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override  //成功
            public void onSuccess(String decode) {
                String result = AllPublic.jsonDesDecode(decode);//解密
                try {
                    JSONObject jsonObject= new JSONObject(result);
                    String reason = jsonObject.getString("code");
                    if (reason.equals("0")) {
                        toastWk("修改密码成功");
                        setResult(1,intent);
                        finish();
                    }else {
                        toastWk(jsonObject.getString(getResources().getString(R.string.errMsg)));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dismissLoad();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogWk("修改密码-第二个："+ex + "");
                ex.printStackTrace();
                dismissLoad();
            }
            @Override
            public void onCancelled(CancelledException cex) {
                LogWk("修改密码-第三个：");
                dismissLoad();
            }
            @Override
            public void onFinished() {
                LogWk("修改密码-第四个：");
                dismissLoad();
            }
        });
    }
    public void sendBR(){
        LogWk("修改成功发送广播");
        Intent intent = new Intent();
        intent.setAction(Constants.MBroadcastReceiver);
        sendBroadcast(intent);
    }
}
